<?php

namespace Miniapp\Controllers;

define('STATUS_NORMAL', 1); //接口正常返回数据
define('STATUS_ERROR', 0);  //接口请求异常

use Phalcon\Mvc\Controller;
use Platform\Libs\Encrypt;

class BaseController extends Controller {

    protected $requestTime;
    protected $responseTime;

    public function initialize() {
        $this->logger->write_log(" ", "info");
        $this->logger->write_log(" ", "info");
        $this->logger->write_log(" ", "info");
        $this->logger->write_log(" ", "info");
        $this->requestTime = ceil(microtime(true) * 1000);
        $this->logger->write_log("requestTime :" . $this->requestTime, "info");
        $url = 'https://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        //日志记录
        $headers = array();
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        $headersStr = "";
        foreach ($headers as $key => $value) {
            $headersStr .= $key . "=>" . $value . "\n";
        }
        $this->logger->write_log(
                "Url: " . $url . "\n" .
                "HttpHeaders: " . $headersStr . "\n"
                , 'info');



        $keyParams = $this->view->getParams();
        $getParams = "";
        foreach ($keyParams as $key => $value) {
            $getParams .= $key . "=>" . $value . "\n";
        }
        $postParams = "";
        foreach ($_POST as $key => $value) {
            $postParams .= $key . "=>" . $value . "\n";
        }
        $this->logger->write_log("\n" .
                "params: " . $getParams . "\n" .
                "postParams: " . $postParams . "\n"
                , 'info');
    }

    /**
     * 返回Api Json数据
     * @param $status
     * @param $data
     * @return mixed
     */
    protected function renderJson($data, $status = 1) {
        $arr = array(
            'status' => $status,
            'data' => $data
        );

        //记录接口返回信息
        @$this->logger->write_log("返回结果:" . var_export($arr, true), "info");
        $arrJson = json_encode($arr);
        $this->logger->write_log("responseTime :" . $this->responseTime, "info");
        $this->logger->write_log("startTime :" . ($this->responseTime - $this->requestTime) . "毫秒", "info");
        echo $arrJson;
        exit;
    }

    protected static function _resError($msg, $status = STATUS_ERROR) {
        $res = array(
            'status' => $status,
            'error' => $msg
        );
        $res = json_encode($res);
        echo $res;
        exit();
    }

    static public function appException($e) {
        //处理一下404错误的错误名称
        switch ($e->getCode()) {
            case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $logger = \Miniapp\Core\Logger::getInstance("empty");
                break;
            default :
                $logger = \Miniapp\Core\Logger::getInstance(__CONTROLLER_NAME__);
                break;
        }
        $log = " 
        " . "错误file: " . $e->getFile() . "
        " . "错误line: " . $e->getLine() . "
        " . "错误code: " . $e->getCode() . "
        " . "错误msg:  " . $e->getMessage() . "
        " . "错误trace: " . $e->getTraceAsString();
        $logger->write_log($log, "error");
        //返回错误信息
        switch ($e->getCode()) {
            case 1404:
                self::_resError($e->getMessage(), 404);
                break;
            case 1405:
                self::_resError($e->getMessage(), 405);
                break;
            default:
                self::_resError($e->getMessage());
        }
    }

}
